package ftp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import com.sun.imageio.plugins.common.InputStreamAdapter;

public class FtpServer {

	public static void main(String[] args) throws Exception{
		final String PATH="ftp/";
		ServerSocket server=new ServerSocket(8001);
		while(true){
			Socket socket=server.accept();
			OutputStream out=socket.getOutputStream();
			InputStream in=socket.getInputStream();
			BufferedReader reader=new BufferedReader(new InputStreamReader(in));
			out.write("please input file name:\n".getBytes());
			
			String filename=reader.readLine();
			File file=new File(PATH+filename);
			while(!file.exists()){
				out.write("file not found,please input file name:\n".getBytes());
				filename=reader.readLine();
				file=new File(PATH+filename);
			}
			
			out.write("200ok\n".getBytes());
			
			InputStream fin=new FileInputStream(file);
			byte[] buf=new byte[fin.available()];
			fin.read(buf);
			out.write(buf);
			
			reader.close();
			out.close();
			fin.close();
			socket.close();
			
		
		}
		

	}

}
